package com.xps.and.yuntong.Utils;

import android.content.Context;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class AssetsUitls {
    private Context c;
    private static AssetsUitls ourInstance = null;

    public static AssetsUitls getInstance(Context con) {
        if (ourInstance == null) {
            synchronized (AssetsUitls.class) {
                ourInstance = new AssetsUitls(con.getApplicationContext());
            }
        }
        return ourInstance;
    }

    private AssetsUitls(Context con) {
        c = con;
    }

    //读取文本文件
    public String getFromAssets(String fileName) throws IOException {
        InputStreamReader inputReader = new InputStreamReader(c.getResources().getAssets().open(fileName), "GBK");
        BufferedReader bufReader = new BufferedReader(inputReader);
        String line = "";
        String Result = "";
        while ((line = bufReader.readLine()) != null) {
            Result += line + "\n";
        }
        return Result;
    }

    /**
     * 从assetes获取所有省份缩写
     */
    public List<String> getProvences() throws IOException {
        List<String> Ps = new ArrayList<>(30);
        String[] ps = getFromAssets("provence.txt").split(" ");
        for (int i = 0; i < ps.length; i++) {
            if (ps[i].length() == 1) {
                Ps.add(ps[i]);
            }
        }
        return Ps;
    }

    /**
     * 从assetes获取所有车型
     */
    public ArrayList<String> getCarbrands() throws IOException {
        ArrayList<String> cbs = new ArrayList<>();
        String[] cb = getFromAssets("carbrand.txt").split("\\n");
        int i = 0;
        for (; i < cb.length; i++) {
            if (cb[i].equals("")) cb[i] = "-";
            cbs.add(i, cb[i]);
        }
        return cbs;
    }

    public void getCarBrands() throws IOException, JSONException {
//        JSONObject datajob = getCarBrandMap();
//        ArrayList<String> keys = getCarBrandSet();
//        HashMap<String, ArrayList<String>> arrayListHashMap = new HashMap<>();
//
//        for (int i = 0; i < datajob.entrySet().size(); i++) {
////            JsonElement je = datajob.get(keys.get(i));
////            JsonArray jsonArray =  je.getAsJsonArray();
////           JUtils.Log(keys.get(i));
//

        getCarBrandMap();
        //Log.i("广汽三菱", String.valueOf(datajob.has("广汽三菱")));
    }

//    //获取汽车品牌set
//    public ArrayList<String> getCarBrandSet() throws IOException, JSONException {
//        ArrayList<String> brandSet = new ArrayList<>();
//
//        Iterator<Set<String, JsonElement>> l = getCarBrandMap().keys();
//
//
//
//        return brandSet;
//    }

    public HashMap<String, ArrayList<String>> getCarBrandMap() throws IOException, JSONException {
        HashMap<String, ArrayList<String>> data = new HashMap<>();
        InputStreamReader inputReader = new InputStreamReader(c.getResources().getAssets().open("CarBrand_json.txt"), "GBK");
        BufferedReader bufReader = new BufferedReader(inputReader);
        String line = "";
        String Result = "";
        while ((line = bufReader.readLine()) != null) {
            Result += line;
        }
        JSONTokener jsonTokener = new JSONTokener(Result);
        JSONObject jsonObject = new JSONObject(jsonTokener);
        Iterator<String> i = jsonObject.keys();

        while (i.hasNext()) {
            String next = i.next();
            ArrayList<String> list = new ArrayList<>();
            JSONArray jsonArray = jsonObject.getJSONArray(next);
            for (int ii = 0; ii < jsonArray.length(); ii++) {
                list.add(jsonArray.get(ii).toString());
            }

            data.put(next, list);
        }

//        Set<String> keys = data.keySet();
//        Iterator<String> ii = keys.iterator();
//        while (ii.hasNext()) {
//            JUtils.Log(ii.next());
//        }
        return data;
    }
}
